<template>
	<view>
		<view class="upload" v-if="(imgUrl.length>0 && disabled) || !disabled">
			<u-upload :fileList="fileList" :previewFullImage="false" @afterRead="afterRead" @delete="deletePic" name="1"
				multiple :maxCount="2" :deletable="!disabled" :disabled="disabled">
				<view style="display: flex;">
					<view class="text">
						上传图片
					</view>
				</view>
			</u-upload>
		</view>
		<view class="upload" v-if="(videoUrl && disabled) || !disabled">
			<u-upload :fileList="fileList1" @afterRead="afterRead1" accept="video" @delete="deleteVid" name="1" multiple
				:maxCount="1" :deletable="!disabled" :disabled="disabled">
				<view style="display: flex;">
					<view class="text">
						上传视频
					</view>
				</view>
			</u-upload>
		</view>
	</view>
</template>

<script>
	import {
		agrServiceUrl
	} from '@/config/baseUrl.js'
	export default {
		data() {
			return {
				fileList: [],
				fileList1: [],
				formData: {
					imgList: [],
					videoList: []
				}
			}
		},
		props: {
			imgUrl: {
				type: Array,
				default: ()=>[]
			},
			videoUrl: {
				type: String,
				default: ()=>''
			},
			disabled: {
				type: Boolean,
				default: ()=>false
			}
		},
		created() {
			this.fileList = []
			this.fileList1 = []
			if(this.imgUrl.length>0) {
				this.imgUrl.forEach(el=>{
					this.fileList.push({
						url: el
					})
				})
			}
			
			if(this.videoUrl) {
				this.fileList1.push({
					url: this.videoUrl
				})
			}
		},
		methods: {
			async afterRead(event) {
				this.type = 'image'
				let lists = [].concat(event.file)
				let fileListLen = this.fileList.length
				lists.map((item) => {
					this.fileList.push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					let item = this.fileList[fileListLen]
					this.fileList.splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: `${agrServiceUrl}${result.slice(1)}`
					}))
					fileListLen++
				}
			},
			async afterRead1(event) {
				this.type = 'video'
				let lists = [].concat(event.file)
				let fileListLen = this.fileList1.length
				lists.map((item) => {
					this.fileList1.push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					let item = this.fileList1[fileListLen]
					this.fileList1.splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: `${agrServiceUrl}${result.slice(1)}`
					}))
					fileListLen++
				}
			},
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: agrServiceUrl + `common/file/upDown/getUploadUrl?moduleType=XG_ORGANIZATION&upDownType=FTP`,
						filePath: url,
						name: 'file',
						header: {
							'token': uni.getStorageSync("satoken") || ''
						},
						formData: {},
						success: async (res) => {
							const dataObj = JSON.parse(res.data);
							const data = dataObj.data;
							console.log(data, 'ww');
							resolve(data);
							if (this.type == 'image') {
								this.formData.imgList.push({
									imgId: data.slice(33),
									imgUrl: `${agrServiceUrl}${data.slice(1)}`
								})
								this.$emit('imageOrVideo', this.formData)
							} else if (this.type == 'video') {
								this.formData.videoList.push({
									videoId: data.slice(33),
									videoUrl: `${agrServiceUrl}${data.slice(1)}`
								})
								this.$emit('imageOrVideo', this.formData)
							}
						},
						fail: () => {
							uni.showToast({
								title: '图片上传失败',
								icon: 'error'
							})
						}
					});
				})
			},
			deletePic(event) {
				this.fileList.splice(event.index, 1)
				this.formData.imgList.splice(event.index, 1)
				this.$emit('imageOrVideo', this.formData)
			},
			deleteVid(event) {
				this.fileList1.splice(event.index, 1)
				this.formData.videoList.splice(event.index, 1)
				this.$emit('imageOrVideo', this.formData)
			}
		},
	}
</script>

<style lang="scss" scoped>
	.upload {
		height: 96rpx;
		display: flex;
		align-items: center;

		.text {
			font-size: 24rpx;
			font-family: Source Han Sans CN-Medium, Source Han Sans CN;
			font-weight: 500;
			color: #2265FB;
			text-decoration: underline;
		}
	}

	/deep/ .u-upload__wrap {
		display: flex;
		align-items: center;
	}

	/deep/ .u-upload__wrap__preview__other {
		height: 80rpx !important;
		width: 80rpx !important;
	}

	/deep/ .u-upload__deletable {
		z-index: 999999999 !important;
	}

	/deep/ .u-textarea {
		width: 99% !important;
	}
</style>